package cn.wolfcode.edu.service.impl;

import cn.wolfcode.edu.domain.Attendance;
import cn.wolfcode.edu.domain.Employee;
import cn.wolfcode.edu.mapper.AttendanceMapper;
import cn.wolfcode.edu.mapper.SalaryMapper;
import cn.wolfcode.edu.query.AttendanceQueryObject;
import cn.wolfcode.edu.service.IAttendanceService;
import cn.wolfcode.edu.util.PageResult;
import cn.wolfcode.edu.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
public class AttendanceServiceImpl implements IAttendanceService {
    @Autowired
    private AttendanceMapper attendanceMapper;
    @Autowired
    private SalaryMapper salaryMapper;
    public PageResult list(AttendanceQueryObject qo) {
        int total = attendanceMapper.queryForCount(qo);
        if(total == 0){
            return new PageResult();
        }
        List<Attendance> rows = attendanceMapper.selectAll(qo);
        return new PageResult(total,rows);
    }
    //签到
    public void signIn(Employee e) throws Exception {
        //查询当前用户是否已签到
        Attendance attendance1 = attendanceMapper.selectInByEmpId(e.getId());
        if( attendance1 != null){
            throw new Exception("你以为今天签两次明天就不用签了？？");
        }
        String realName = e.getRealName();
        Attendance attendance = new Attendance();

        Date date = new Date();
        attendance.setAttendanceDate(date);
        String time = "09:00:00";
        if(TimeUtil.compare(time)){
        attendance.setInstatus(true);
        }
        else{
            attendance.setInstatus(false);
            salaryMapper.updateLate(e.getId());
        }
        attendance.setSignInTime(new Date());
        attendance.setEmployee(e);
        attendanceMapper.signIn(attendance);
        salaryMapper.addWorkDays(e.getId());
    }
    //签退
    public void signBack(Employee e) throws Exception {
        //查询数据库中是否拥有当天的签到记录 有则更新这条数据 没有则提示
        Date date = new Date();
        Attendance attendance = attendanceMapper.selectInByEmpId(e.getId());
        if(attendance == null){
            throw new Exception("你还未签到呢");
        }
        /*attendance = attendanceMapper.selectBackByEmpId(e.getId());*/
            if(attendance.getSignBackTime()!=null){
                throw new Exception("不能重复签退");
            }
            attendance.setSignBackTime(new Date());
            String time = "18:00:00";
            if(!TimeUtil.compare(time)){
            attendance.setBackStatus(true);
                salaryMapper.updateEarly(e.getId());
            }
            else{
                attendance.setBackStatus(false);
            }
            attendance.setEmployee(e);
            attendanceMapper.signBack(attendance);
        //如果为新的一天则插入数据
       /* else{
            String realName = e.getRealName();
            Attendance attendance1 = new Attendance();
            attendance1.setEmployee(e);
            attendance1.setBackStatus(true);
            attendance1.setAttendanceDate(new Date());
            attendance1.setSignBackTime(new Date());
            attendanceMapper.insert(attendance1);
        }*/
    }
}
